﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESOL.AutoUpdate.EsolWebService;
using System.Net;
using System.Reflection;

namespace ESOL.AutoUpdate
{
    public partial class frmStart : frmBaseForm
    {
        private string mExeFileName = "ESOL.WinUI.exe";

        public frmStart()
        {
            InitializeComponent();
        }

        private void StartRunApplication()
        {
            if (File.Exists(Application.StartupPath + "\\" + mExeFileName) == false)
            {
                return;
            }
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = Application.StartupPath + "\\" + mExeFileName;
            myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            myProcess.Start();
        }

        /// <summary>
        /// 判断是否需要下载
        /// </summary>
        /// <returns></returns>
        private bool NeedUpdate()
        {
            if (File.Exists(Application.StartupPath + "\\" + mExeFileName) == false)
            {
                //如果运行目录下的执行文件不存在，则需要下载
                //return true;
                using (frmHostSetting frm = new frmHostSetting())
                {
                    DialogResult res = frm.ShowDialog();
                    if (res == DialogResult.OK)
                    {
                        return true;
                    }
                    else
                    {
                        this.Close();
                    }
                }
                //frmHostSetting frm = new frmHostSetting();
                
            }
            MemoryStream memStream;
            using (FileStream stream = new FileStream(Application.StartupPath + "\\" + mExeFileName, FileMode.Open))
            {
                using (memStream = new MemoryStream())
                {
                    int res;
                    byte[] b = new byte[4096];
                    while ((res = stream.Read(b, 0, b.Length)) > 0)
                    {
                        memStream.Write(b, 0, b.Length);
                    }
                }
            }
            Assembly assDllLocal = Assembly.Load(memStream.ToArray());
            string latestVersion = "";
            using (EsolService service = new EsolService())
            {
                latestVersion = service.GetLatestVersion();
            }
            if (assDllLocal.GetName().Version.ToString() != latestVersion)
            {
                //版本号不同，则需要下载
                memStream.Dispose();
                return true;
            }
            memStream.Dispose();
            return false;
        }

        private void frmStart_Load(object sender, EventArgs e)
        {
            try
            {
                //判断是否需要下载
                bool needUpdate = NeedUpdate();

                if (needUpdate == false)
                {
                    StartRunApplication();
                    this.Close();
                }
                else
                {
                    frmUpdate frm = new frmUpdate();
                    frm.ShowDialog();
                    StartRunApplication();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
    }
}
